from OpenGL.arrays import vbo
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from numpy import array
from collections import namedtuple
import sys, math, os

Point = namedtuple('POINT', ['x', 'y'], verbose=True)

p1 = Point(1,0) # 1
p2 = Point(2,2) # 2
p3 = Point(3,0) # 3 
p4 = Point(4,2) # 4
p5 = Point(5,0) # 5
p6 = Point(6,2) # 6
p7 = Point(5,4) # 7
p8 = Point(7,4) # 8
p9 = Point(8,2) # 9
p10 = Point(9,4) # 10
p11 = Point(10,2)# 11
p12 = Point(9,0) # 12
p13 = Point(11,0)# 13

##points = [1,2,3,4,5,4,6,7,6,8,9,10,9,11,12,13]
points = [p1,p2,p3,p4,p5,p4,p6,p7,p6,p8,p9,p10,p9,p11,p12,p13]
          
vbo = vbo.VBO(array(points, 'f')) # vbo an Graka schicken

def init(width, height):
   """ Initialize an OpenGL windown and rendering context"""
   glClearColor(1, 1, 1, 1)         # background color
   glMatrixMode(GL_PROJECTION)      # switch to projection matrix
   glLoadIdentity()                 # set to 1
   # Sichtfeld (Projektionsart)
##   gluOrtho2D(-1, 15,               # links, rechts clipping planes
##              -1, 15)               # unten, oben  clipping planes
   gluPerspective(45.,           # oeffnungswinkel fieldofview fovy
                  width/height,  # aspect ratio
                  0.1,           # near
                  100.           # far
                  )
   # Objekte
   glMatrixMode(GL_MODELVIEW)       # switch to modelview matrix
   
   
def display():
   """Rendering einstellungen"""
   glClear(GL_COLOR_BUFFER_BIT,  # resettet angegebenen Buffer
           GL_DEPTH_BUFFER_BIT
           )
   glLoadIdentity()              # Matrix resetten
   gluLookAt(5, 0, 20,           # eye                
             5, 0, -4,           # center (x koord selbe wie eye sonst schief)
             0, 1, 0             # up
             )
   glColor3f(0,0,0)
   glPolygonMode(GL_FRONT_AND_BACK,
                 GL_LINE)
   # Render Vertex Buffer Object
   vbo.bind()
   glVertexPointerf(vbo)
   glEnableClientState(GL_VERTEX_ARRAY)
   glDrawArrays(GL_TRIANGLE_STRIP, 0, 16) # !! Anzahl Arrayelemente mit doppelten Kanten beachten
   vbo.unbind()
   glDisableClientState(GL_VERTEX_ARRAY)
   glFlush() # Flush commands     
   
 
def reshape():
   pass
   
def main():
   width, height = 500, 500
   glutInit(sys.argv)
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB )   
   glutInitWindowSize(width, height)
   glutCreateWindow("Test OPENGL Fensterchen")
   glutDisplayFunc(display)     #register display function

   
   # Hack for Mac OS X
#   cwd = os.getcwd()
#   glutInit(sys.argv)
#   os.chdir(cwd)

   init(width, height)#initialize OpenGL state and opengl context
   glutMainLoop()   #start even processing

if __name__ == "__main__":
   main()
